package com.item.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 *
 * @author zhang-rongyao
 * @version V1.0
 * @Package com.item.model
 * @date 2021/7/27 10:22
 */
@SpringBootTest
@Slf4j
public class HHH {

    List<Person> personList = new ArrayList<Person>();
    @PostConstruct
    void aa(){
        personList.add(new Person("Tom", 8900, "male", "New York"));
        personList.add(new Person("Jack", 7000, "male", "Washington"));
        personList.add(new Person("Lily", 7800, "female", "Washington"));
        personList.add(new Person("Anni", 8200, "female", "New York"));
        personList.add(new Person("Owen", 9500, "male", "New York"));
        personList.add(new Person("Alisa", 7900, "female", "New York"));

    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    class Person {
        private String name;  // 姓名
        private int salary; // 薪资
        private int age; // 年龄
        private String sex; //性别
        private String area;  // 地区

        public Person(String name, int salary, String sex, String area) {
            this.name = name;
            this.salary = salary;
            this.sex = sex;
            this.area = area;
        }
    }


    @Test
    public void method2(){

        //System.out.println(personList);
        List<String> collect = personList.stream().filter(x -> "female".equals(x.sex)).map(Person::getName).collect(Collectors.toList());
        System.out.println(collect);


    }

    @Test
    public void method(){

        //System.out.println(personList);
        //personList.stream().filter(sex -> sex.equals("female")).forEach(System.out::println);
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        list.stream().filter(x->x>7).forEach(System.out::println);
        // 匹配第一个
        Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
        // 匹配任意（适用于并行流）
        Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
        // 是否包含符合特定条件的元素
        boolean anyMatch = list.stream().anyMatch(x -> x < 6);
        System.out.println("匹配第一个值：" + findFirst.get());
        System.out.println("匹配任意一个值：" + findAny.get());
        System.out.println("是否存在大于6的值：" + anyMatch);


    }


}
